package HelloSeries.ui.prefs.elems.flextables.editor;

import java.awt.Color;
import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;


public class IntegerEditor extends AbstractCellEditor implements TableCellEditor {
	protected JTextField textfield;
	private String    content;
	private JTable table;

	public IntegerEditor() {
		super();
		textfield = new JTextField();
	}

	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		this.table = table;
		textfield = (JTextField) value;
		content = textfield.getText();
		return textfield;
	}

	public Object getCellEditorValue() {
		content = textfield.getText();
		return content;
	}
	public boolean stopCellEditing() {
		content = textfield.getText();
		if (content.equals("")) super.cancelCellEditing();
		else if (!isInteger(content)) {
			textfield.setBackground(Color.RED);
			return false;
		}
		return super.stopCellEditing();
	}

	public boolean isInteger(String s) {
		boolean isValid = true;
		try{ Integer.parseInt(s); }
		catch(NumberFormatException nfe){ isValid = false; }
		return isValid;
	}
}
